package auto.feature;


import auto.common.ImageConvertedBase64;
import auto.device.Phione;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.Rectangle;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


import java.util.HashMap;
import java.util.Map;


public class ChatListButton {

    //feature1:聊天列表按钮功能检测
    public String chatListButtonTest() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;
        //点击进入聊天列表
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"聊天\"`]"))).click();

        String result=addButtonTest(driver,wait);
        if(!result.equals("正常")){
            return result;
        }

        result=searchBoxTest(driver,wait);
        if(!result.equals("正常")){
            return result;
        }

        result=editButtonTest(wait);
        if(!result.equals("正常")){
            return result;
        }

        return "聊天列表按钮显示/功能正常";
    }


    //"+" 按钮检测
    private String addButtonTest(AppiumDriver driver,WebDriverWait wait) {
        //首先检测"+"按钮
        try {
            WebElement addButton = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                    ("**/XCUIElementTypeNavigationBar[`name == \"聊天\"`]/XCUIElementTypeButton[2]")));
            addButton.click();

            try {
                String newGroupBase64=ImageConvertedBase64.encodeImageToBase64("src/main/resources/image/newGroup.png");
                WebElement newGroup = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.image(newGroupBase64)));

                String sweepBase64=ImageConvertedBase64.encodeImageToBase64("src/main/resources/image/sweep.png");
                WebElement sweep = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.image(sweepBase64)));

                String addContactBase64=ImageConvertedBase64.encodeImageToBase64("src/main/resources/image/addFriend.png");
                WebElement addContact = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.image(addContactBase64)));
                System.out.println("addBottun按钮内选项正常");
            } catch (TimeoutException e) {
                System.out.println("\"+\"按钮内选项缺失");
                return "\"+\"按钮内选项缺失";
            }

            //点击原来的位置取消这个选项
            // 获取元素的位置和大小
            Rectangle rect = addButton.getRect();

            // 计算元素中心点的坐标
            int centerX = rect.x + (rect.width / 2);
            int centerY = rect.y + (rect.height / 2);

            // 使用 executeScript 来执行点击操作
            Map<String, Object> params = new HashMap<>();
            params.put("x", centerX);
            params.put("y", centerY);
            driver.executeScript("mobile: tap", params);
        } catch (TimeoutException e) {
            System.out.println("\"+\"按钮缺失");
            return "\"+\"按钮缺失";
            // 可以在这里添加其他的错误处理逻辑
        }
        return "正常";
    }


    //搜索栏检测
    private String searchBoxTest(AppiumDriver driver,WebDriverWait wait) throws InterruptedException {
        //连续下滑三次看一看搜索栏是不是出现了
        Map<String, Object> argsDown = new HashMap<>();
        argsDown.put("direction", "down");
        int i = 0;

        for (; i < 5; i++) {
            driver.executeScript("mobile: swipe", argsDown);

            if((driver.findElements(AppiumBy.accessibilityId("搜索")).size()>0)){
                break;
            }
        }
        if(i==5){
            return"搜索栏丢失";
        }
        Thread.sleep(1000);

        //点击联系人列表搜索栏
        WebElement searchBox=driver.findElement(AppiumBy.accessibilityId("搜索"));
        searchBox.click();
        searchBox.sendKeys("输入测试");

        //tab选项-媒体
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("媒体"))).click();
        }catch (TimeoutException e){
            System.out.println("tab选项-媒体不存在");
            return "tab选项-媒体不存在";
        }

        //tab选项-文件
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("文件"))).click();
        }catch (TimeoutException e){
            System.out.println("tab选项-文件不存在");
            return "tab选项-文件不存在";
        }

        //tab选项-音乐
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("音乐"))).click();
        }catch (TimeoutException e){
            System.out.println("tab选项-音乐不存在");
            return "tab选项-音乐不存在";
        }

        //tab选项-语音
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("语音"))).click();
        }catch (TimeoutException e){
            System.out.println("tab选项-语音不存在");
            return "tab选项-语音不存在";
        }

        //tab选项-链接
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("链接"))).click();
        }catch (TimeoutException e){
            System.out.println("tab选项-链接不存在");
            return "tab选项-链接不存在";
        }

        //聊天列表-搜索栏内容清除按钮
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("清除文本"))).click();
        }catch (TimeoutException e){
            System.out.println("聊天列表-搜索栏内容清除按钮消失");
            return "聊天列表-搜索栏内容清除按钮消失";
        }

        //日历按钮
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("icon input date"))).click();
            try{
                wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"取消\"`]"))).click();
            }catch (TimeoutException e){
                System.out.println("日历页面取消按钮消失");
                return"日历页面取消按钮消失";
            }

        }catch (TimeoutException e){
            System.out.println("日历按钮消失");
            return "日历按钮消失";
        }

        //对话/联系人选择按钮
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("icon input viewchat"))).click();

            //对话/联系人选择页面-切换联系人/对话
            try{
                wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("联系人"))).click();
                wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("对话"))).click();
            }catch (TimeoutException e){
                System.out.println("对话/联系人选择页面-切换联系人/对话失效");
                return "对话/联系人选择页面-切换联系人/对话功能异常";
            }

            //对话/联系人选择页面-搜索栏
            try{
                WebElement internalSearchBox=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.xpath
                        ("(//XCUIElementTypeSearchField[@name=\"搜索\"])[2]")));
                internalSearchBox.click();
                internalSearchBox.sendKeys("联系人/对话输入测试");
            }catch (TimeoutException e){
                System.out.println("对话/联系人选择页面-搜索栏功能异常");
                return "对话/联系人选择页面-搜索栏功能异常";
            }


            //对话/联系人选择页面-搜索栏清除内容按钮
            try{
                wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("清除文本"))).click();
            }catch (TimeoutException e){
                System.out.println("对话/联系人选择页面-搜索栏清除内容按钮缺失");
                return "对话/联系人选择页面-搜索栏清除内容按钮缺失";
            }

            //对话/联系人选择页面-搜索栏取消按钮
            try{
                WebElement back=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.xpath
                        ("(//XCUIElementTypeButton[@name=\"取消\"])[2]")));
                back.click();
            }catch (TimeoutException e){
                System.out.println("对话/联系人选择页面-搜索栏取消按钮缺失");
                return "对话/联系人选择页面-搜索栏取消按钮缺失";
            }


        }catch (TimeoutException e){
            System.out.println("联系人/会话选择按钮消失");
            return "联系人/会话选择按钮消失";
        }


        //选择联系人/会话-选择页面取消使用按钮
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.xpath
                    ("(//XCUIElementTypeButton[@name=\"取消\"])[2]"))).click();
        }catch (TimeoutException e){
            System.out.println("选择页面取消使用按钮缺失");
            return "选择联系人/会话-选择页面取消使用按钮缺失";
        }

        Thread.sleep(500);
        //联系人列表搜索栏取消按钮
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"取消\"`]"))).click();
        }catch (TimeoutException e){
            System.out.println("取消按钮消失");
            return "取消按钮消失";
        }

        return "正常";
    }

    //编辑按钮检测
    private String editButtonTest(WebDriverWait wait) throws InterruptedException {
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"编辑\"`]"))).click();
            try{
                wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]" +
                        "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                        "/XCUIElementTypeOther[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                        "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]/XCUIElementTypeButton")));
            }catch (TimeoutException e){
                System.out.println("编辑功能-勾选框消失");
                return "编辑功能勾选框消失";
            }

        }catch(TimeoutException e){
            System.out.println("聊天列表-编辑按钮缺失");
            return "聊天列表-编辑按钮缺失";
        }

        Thread.sleep(500);
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"取消\"`]"))).click();
        }catch (TimeoutException e){
            System.out.println("点击编辑后，取消按钮没有显示");
            return "点击编辑后，取消按钮没有显示";
        }
        return "正常";
    }
}







